package com.mlx.mbg.code.method;


import com.mlx.mbg.code.config.MbgPropertyConfig;
import com.mlx.mbg.code.inter.EnableMethod;
import com.mlx.mbg.code.inter.ExtensionLevel;
import com.mlx.mbg.code.inter.IExtensionMethod;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.*;

@EnableMethod
public class UpdateMethod implements IExtensionMethod {

    private final IExtensionMethod referenceMethod = new UpdateByPrimaryKeySelectiveMethod();

    @Override
    public boolean checkConfig(ExtensionLevel level, MbgPropertyConfig mbgPropertyConfig, IntrospectedTable introspectedTable) {
        boolean checkReferenceMethodConfig = referenceMethod.checkConfig(level, mbgPropertyConfig, introspectedTable);
        boolean isContains = getExtensionLevelSet().contains(level);
        return checkReferenceMethodConfig && isContains;
    }

    @Override
    public int getOrder() {
        return 1530;
    }

    @Override
    public Set<ExtensionLevel> getExtensionLevelSet() {
        Set<ExtensionLevel> levelSet = new HashSet<>();
        levelSet.add(ExtensionLevel.SERVICE);
        levelSet.add(ExtensionLevel.CONTROLLER);
        return levelSet;
    }

    @Override
    public List<Method> buildMethod(IntrospectedTable introspectedTable) {
        List<Method> methodList = referenceMethod.buildMethod(introspectedTable);
        Method method = methodList.get(0);
        method.setName("update");

        List<String> javaDocLines = method.getJavaDocLines();
        javaDocLines.clear();
        // 设置备注
        method.addJavaDocLine("/**");
        method.addJavaDocLine(" * 修改");
        method.addJavaDocLine(" */");
        return methodList;
    }

    @Override
    public List<XmlElement> buildSQL(IntrospectedTable introspectedTable) {
        return new ArrayList<>();
    }
}
